;**************************************************************************** 
;Copyright © 2008-2011 Oregon State University                                
;All Rights Reserved.                                                         
;                                                                             
;                                                                             
;Permission to use, copy, modify, and distribute this software and its        
;documentation for educational, research and non-profit purposes, without     
;fee, and without a written agreement is hereby granted, provided that the    
;above copyright notice, this paragraph and the following three paragraphs    
;appear in all copies.                                                        
;                                                                             
;                                                                             
;Permission to incorporate this software into commercial products may be      
;obtained by contacting Oregon State University Office of Technology Transfer.
;                                                                             
;                                                                             
;This software program and documentation are copyrighted by Oregon State      
;University. The software program and documentation are supplied "as is",     
;without any accompanying services from Oregon State University. OSU does not 
;warrant that the operation of the program will be uninterrupted or           
;error-free. The end-user understands that the program was developed for      
;research purposes and is advised not to rely exclusively on the program for  
;any reason.                                                                  
;                                                                             
;                                                                             
;IN NO EVENT SHALL OREGON STATE UNIVERSITY BE LIABLE TO ANY PARTY FOR DIRECT, 
;INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST      
;PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 
;IF OREGON STATE UNIVERSITYHAS BEEN ADVISED OF THE POSSIBILITY OF SUCH        
;DAMAGE. OREGON STATE UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,       
;INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
;FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY OF               
;NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,    
;AND OREGON STATE UNIVERSITY HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,       
;SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.                            
;                                                                             
;**************************************************************************** 

;
; Copyright (c) 1998, Oregon State University.
; This software is currently in beta-test condition.  It may not be altered,
;	copied, or redistributed without express written permission of
;	Robert E. Kennedy, Oregon State University (kennedyr@fsl.orst.edu).
;	This software is supplied as is, with no express or implied
;	warranties.

function stretchit, variable, percent=percent, ignore=ignore, $
	getrange=getrange, setrange=setrange, maxoutval = maxoutval


;set maxoutvall if you want the highest number to be something
;   other than 255


on_error,2


if n_elements(maxoutval) eq 0 then maxoutval = 255



var = variable

;If the user wants to ignore some values, take care of that here

if n_elements(ignore) ne 0 then begin
	igs = where(variable eq ignore, numigs)
	use = where(variable ne ignore)
	var = var(use)
	end


maxv=max(var)
minv=min(var)

sz = size(var, /type)
if sz eq 4 then begin
   r = range(var)
   if r lt 300 then var = var * 100
   if r gt 32700 then var = var / 100
end
var = fix(var)	;make integer for histogram

if n_elements(percent) ne 0 then begin
   h = histogram(var, omin=om)
   for i=1l,n_elements(h)-1 do h[i] = h[i]+h[i-1]
   t = n_elements(var)
   break = t*percent*.01
   minv = max(where(h le break))+om
   maxv = min(where(h ge t-break))+om
end

if n_elements(setrange) ne 0 then begin
	minv=setrange[0]
	maxv=setrange[1]
	end

var = (var - minv) * (float(maxoutval)/(maxv-minv))


r = where(var lt 0, many)
if many ne 0 then var(r) = 0
r = where(var gt maxoutval, many)
if many ne 0 then var(r) = maxoutval

;Again, if there were things to ignore, we need to
;	put the array back together again

if n_elements(ignore) ne 0 then begin
	z = variable
	z(use) = var
	var=z
	end

getrange = [minv, maxv]



var=byte(var)


return, var
end
